<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
    <title>新建博客</title>

    <!--layui插件所需js/css-->
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../common/layui/css/layui.css" media="all">
    <script src="../common/layui/layui.js"></script>

    <!--ueditor插件所需js/css-->
    <!--<script type="text/javascript" charset="utf-8" src="/Blog/common/ueditor/ueditor.config.js"></script>-->
    <!--<script type="text/javascript" charset="utf-8" src="/Blog/common/ueditor/ueditor.all.min.js"> </script>-->
    <!--<script type="text/javascript" charset="utf-8" src="/Blog/common/ueditor/lang/zh-cn/zh-cn.js"></script>-->
    <script type="text/javascript" charset="utf-8" src="../common/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="../common/ueditor/ueditor.all.min.js"></script>
    <script type="text/javascript" charset="utf-8" src="../common/ueditor/lang/zh-cn/zh-cn.js"></script>


    <style type="text/css">
        .content {
            width: 95%;
            align-content: center;
        }

        .blog-content {
            z-index: 0;
        }

        .layui-form-item {
            margin-left: 60px;
        }

    </style>
</head>
<body>
<div class="content">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新建博文</legend>
    </fieldset>

    <form class="layui-form" action="" type="post">
        <div class="layui-form-item">
            <label class="layui-form-label">博文标题</label>
            <div class="layui-input-block">
                <input type="text" name="title" lay-verify="title" autocomplete="off" placeholder="请输入博文标题"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">博文作者</label>
            <div class="layui-input-block">
                <input type="text" name="author" lay-verify="required" placeholder="请输入" autocomplete="off"
                       class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">博文类别</label>
            <div class="layui-input-block blog-type">
                <select name="type">
                    <option value=""></option>
                    <option value="0">Java</option>
                    <option value="1" selected="">Linux</option>
                    <option value="2">C++</option>
                    <option value="3">Html</option>
                    <option value="4">JavaScript</option>
                    <option value="5">CSS</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label>博文内容</label>
            <!--加载ueditor插件-->
            <div class="layui-input-block blog-content" name="content" lay-verify="content">
                <script id="editor" type="text/plain" style="width:90%;height:500px;"></script>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="addBlog">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>

    </form>
</div>
<script type="text/javascript">
    layui.use(['form', 'layedit', 'laydate', 'jquery'], function () {
        var form = layui.form
                , layer = layui.layer
                , layedit = layui.layedit
                , laydate = layui.laydate,
                $ = layui.jquery;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });

        // UE.getEditor('editor').setHeight(500);
        var ue = UE.getEditor('editor');
        ue.ready(function () {
            ue.setHeight(500);
        });


        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            },
            content: function (value) {
                if (!UE.getEditor('editor').hasContents()) {
                    return '请填写博文内容！';
                }
            }
        });


        //监听提交
        form.on('submit(addBlog)', function (data) {
            console.log(data.field);
            $.post("/servlet/addBlog", data.field, function (result) {
                console.log(result);
                // console.log(result.msg);
                // layer.msg(result.msg);
                if ("添加成功！" == result) {
                    layer.alert(result, {icon: 6});
                    // location.reload();
                } else {
                    layer.alert(result, {icon: 5});
                }

            });

            return false;
        });


    });
</script>
</body>
</html>